<template>
  <div class="putshelf">
    <span style="font-size: 18px">{{ title }}</span>
    <div class="bgan">
      <div class="mintitle">
        请选择您要上架的商品
        <div>
          <el-input
            v-model="searchtext"
            placeholder="请输入商品名称"
            style="width: 200px; margin-right: 10px"
          ></el-input>
          <el-button type="primary" icon="el-icon-search" @click="search"
            >搜索</el-button
          >
          <el-button type="primary" plain @click="reset()">重置</el-button>
        </div>
        <el-button type="primary" style="width: 100px" @click="addcommodity"
          >添加商品</el-button
        >
      </div>
      <div>
        <el-table
          :data="
            tableData.slice(
              (currentPage - 1) * pageSize,
              currentPage * pageSize
            )
          "
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-form label-position="left" inline class="demo-table-expand">
                <el-form-item label="商品名称">
                  <span>{{ props.row.name }}</span>
                </el-form-item>
                <el-form-item label="所属店铺">
                  <span>{{ props.row.shop }}</span>
                </el-form-item>
                <el-form-item label="商品 ID">
                  <span>{{ props.row.id }}</span>
                </el-form-item>
                <el-form-item label="店铺 ID">
                  <span>{{ props.row.shopid }}</span>
                </el-form-item>
                <el-form-item label="商品分类">
                  <span>{{ props.row.category }}</span>
                </el-form-item>
                <el-form-item label="店铺地址">
                  <span>{{ props.row.address }}</span>
                </el-form-item>
                <el-form-item label="商品描述">
                  <span>{{ props.row.de }}</span>
                </el-form-item>
                <el-form-item label="库存数量">
                  <span>{{ props.row.quantity }}</span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column prop="comimg" label="主图" width="180">
            <template slot-scope="scope">
              <el-image
                style="
                  width: 80px;
                  max-heigth: 80px;
                  display: block;
                  margin: 0 auto;
                "
                @click="enlarge(scope.row)"
                :src="scope.row.comimg"
                ><div slot="error" class="image-slot">没有图片</div></el-image
              >
            </template>
          </el-table-column>
          <el-table-column label="商品 ID" prop="id" width="120">
          </el-table-column>
          <el-table-column label="商品名称" prop="name"> </el-table-column>
          <el-table-column label="描述" prop="de"> </el-table-column>
          <el-table-column label="操作" fixed="right" width="300">
            <template slot-scope="scope">
              <el-button
                size="mini"
                @click="handleEdit(scope.$index, scope.row)"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)"
                >删除</el-button
              >
              <el-button size="mini" type="primary" @click="choice(scope.row)">
                上架
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div>
        <el-dialog
          title="修改数据"
          :visible.sync="edit"
          width="30%"
          center
          style="overflow-y: scroll; height: 100vh"
        >
          <el-form ref="editform" :model="form" label-width="80px">
            <el-form-item label="商品id">
              <el-input v-model="form.id" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="商品名称">
              <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="描述">
              <el-input v-model="form.de"></el-input>
            </el-form-item>
            <el-form-item label="商品分类">
              <el-input v-model="form.category"></el-input>
            </el-form-item>
            <el-form-item label="店铺名称">
              <el-input v-model="form.shop"></el-input>
            </el-form-item>
            <el-form-item label="店铺id">
              <el-input v-model="form.shopid"></el-input>
            </el-form-item>
            <el-form-item label="店铺地址">
              <el-input v-model="form.address"></el-input>
            </el-form-item>
            <el-form-item label="库存数量">
              <el-input v-model="form.quantity"></el-input>
            </el-form-item>
            <el-form-item label="主图">
              <el-image
                style="
                  width: 80px;
                  max-heigth: 80px;
                  display: block;
                  margin: 0 auto;
                "
                :src="form.comimg"
                ><div slot="error" class="image-slot">没有图片</div></el-image
              >
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="edit = false">取 消</el-button>
            <el-button type="primary" @click="editrd">确 定</el-button>
          </span>
        </el-dialog>
      </div>
      <div>
        <el-dialog
          title="添加一个商品"
          :visible.sync="dialogVisible"
          width="30%"
          :before-close="handleClose"
        >
          <div>
            <el-steps :active="active">
              <el-step title="步骤 1" description="输入商品相关信息"> </el-step>
              <el-step title="步骤 2" description="填写商家相关信息"></el-step>
              <el-step title="步骤 3" description="等待管理员审核"></el-step>
            </el-steps>
            <div style="margin-top: 10px">
              <el-form ref="form" :model="comform" label-width="80px">
                <div v-show="active == 1">
                  <el-form-item label="商品ID">
                    <el-input v-model="comform.comid" placeholder="请输入" />
                  </el-form-item>
                  <el-form-item label="商品名称">
                    <el-input v-model="comform.name" placeholder="请输入" />
                  </el-form-item>
                  <el-form-item label="商品分类">
                    <el-select
                      v-model="comform.classification"
                      placeholder="请选择"
                    >
                      <el-option
                        v-for="item in options"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                      >
                      </el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item label="商品描述">
                    <el-input v-model="comform.de" placeholder="请输入" />
                  </el-form-item>
                  <el-form-item label="库存数量">
                    <el-input v-model="comform.quantity" placeholder="请输入" />
                  </el-form-item>
                  <el-form-item label="商品图片">
                    <el-upload
                      class="upload-demo"
                      action="https://jsonplaceholder.typicode.com/posts/"
                      :on-preview="handlePreview"
                      :on-remove="handleRemove"
                      :file-list="fileList"
                      list-type="picture"
                    >
                      <el-button size="small" type="primary"
                        >点击上传</el-button
                      >
                      <div slot="tip" class="el-upload__tip">
                        只能上传jpg/png文件,且不超过500kb
                      </div>
                    </el-upload>
                  </el-form-item>
                </div>
                <div v-show="active == 2">
                  <el-form-item label="店铺ID">
                    <el-input v-model="comform.shopid" placeholder="请输入" />
                  </el-form-item>
                  <el-form-item label="店铺名称">
                    <el-input v-model="comform.busname" placeholder="请输入" />
                  </el-form-item>
                  <el-form-item label="店铺位置">
                    <el-input v-model="comform.site" placeholder="请输入" />
                  </el-form-item>
                </div>
                <div v-show="active == 3">
                  <span style="font-size: 18px; color: #f56c6c; padding: 10px"
                    >审核将会在两个工作日内出结果</span
                  >
                </div>
                <el-button style="margin-top: 12px" @click="pre"
                  >上一步</el-button
                >
                <el-button style="margin-top: 12px" type="primary" @click="next"
                  >下一步</el-button
                >
              </el-form>
            </div>
          </div>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addcom()">确 定</el-button>
          </span>
        </el-dialog>
      </div>
      <div class="block" style="margin-top: 15px">
        <el-pagination
          align="center"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="tableData.length"
        >
        </el-pagination>
      </div>
      <div class="enl">
        <el-dialog title="大图预览" :visible.sync="enl" width="45%">
          <el-image
            :src="big_img"
            style="
              width: 600px;
              max-height: 600px;
              margin: 0 auto;
              display: block;
              border-radius: 4px;
            "
          ></el-image>
        </el-dialog>
      </div>
      <div v-loading.fullscreen.lock="fullscreenLoading"></div>
      <div>
        <el-dialog title="验证密码" :visible.sync="pasd" width="30%">
          <el-input
            placeholder="请输入上架密码"
            v-model="input"
            show-password
          ></el-input>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="pushcom()">确 定</el-button>
          </span>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      title: "商品上架",
      searchtext: "",
      pasd: false,
      input: "",
      fullscreenLoading: true,
      edit: false,
      dialogVisible: false,
      enl: false,
      big_img: "",
      active: 1,
      border: "",
      fileList: [],
      isIndeterminate: true,
      checked1: true,
      checked2: false,
      checked3: false,
      checked4: false,
      form: [],
      tableData: [],
      comform: {
        comid: "", //商品id
        name: "", //商品名
        classification: "", //商品分类
        de: "", //商品描述
        comimg: "", //商品图片
        shopid: "", //店铺id
        busname: "", //店铺名
        site: "", //店铺位置
        quantity: "", //库存数量
      },
      options: [
        //分类选项
        {
          value: "T恤",
          label: "T恤",
        },
        {
          value: "卫衣",
          label: "卫衣",
        },
        {
          value: "裙子",
          label: "裙子",
        },
        {
          value: "衬衫",
          label: "衬衫",
        },
        {
          value: "裤子",
          label: "裤子",
        },
        {
          value: "睡衣",
          label: "睡衣",
        },
        {
          value: "鞋子",
          label: "鞋子",
        },
        {
          value: "帽子",
          label: "帽子",
        },
        {
          value: "外套",
          label: "外套",
        },
        {
          value: "袜子",
          label: "袜子",
        },
        {
          value: "包包",
          label: "包包",
        },
        {
          value: "腰带",
          label: "腰带",
        },
        {
          value: "眼镜",
          label: "眼镜",
        },
        {
          value: "围巾",
          label: "围巾",
        },
        {
          value: "项链",
          label: "项链",
        },
      ],
      value: "",
      multipleSelection: [],
      currentPage: 1, //默认页
      pageSize: 4,
      zc: [],
    };
  },
  mounted() {
    this.checkbod();
  },
  methods: {
    enlarge(row) {
      this.enl = true;
      this.big_img = row.comimg;
    },
    selects() {
      this.$axios
        .get("http://43.142.118.44/backstorg/notcom.php", {})
        .then((res) => {
          this.tableData = res.data;
          this.fullscreenLoading = false;
        });
    },
    handleSelectionChange(val) {
      //选框
      this.multipleSelection = val;
    },
    search() {
      //搜索
      if (this.searchtext !== "") {
        this.$axios
          .get("http://43.142.118.44/backstorg/searchnot.php", {
            params: {
              name: this.searchtext,
            },
          })
          .then((res) => {
            this.tableData = res.data;
            console.log(res.data);
          });
      } else {
        this.$message.error("请输入您要搜索的关键字");
      }
    },
    reset() {
      //重置表格
      this.selects();
      this.searchtext = "";
    },
    choice(row) {
      //上架确认
      this.pasd = true;
      this.zc = row;
      console.log(this.zc);
    },
    pushcom() {
      if (this.input == "1234") {
        console.log(this.zc)
        this.$axios
          .get("http://43.142.118.44/backstorg/pushcom.php", {
            params: {
              id: this.zc.id,
              name: this.zc.name,
              category: this.zc.category,
              de: this.zc.de,
              shop: this.zc.shop,
              shopid: this.zc.shopid,
              quantity: this.zc.quantity,
              address: this.zc.address,
              comimg: this.zc.comimg,
            },
          })
          .then((res) => {
            console.log(res.data);
            if (res.data == true) {
              this.$message({
                type: "success",
                message: "上架成功",
              });
              this.selects();
            } else {
              this.$message({
                type: "error",
                message: "上架失败,请检查商品id是否冲突",
              });
            }
          });
      } else {
        this.$message({
          type: "error",
          message: "密码错误",
        });
      }
    },
    checkbod() {
      if (this.comList[0].checked == true) {
        this.border = "1px solid #409EFF";
      }
    },
    addcommodity() {
      this.dialogVisible = true;
    },
    addcom() {
      this.$axios
        .get("http://43.142.118.44/backstorg/addcom.php", {
          params: {
            id: this.comform.comid,
            name: this.comform.name,
            category: this.comform.classification,
            de: this.comform.de,
            shop: this.comform.busname,
            shopid: this.comform.shopid,
            address: this.comform.site,
            quantity: this.comform.quantity,
            comimg: this.comform.comimg,
          },
        })
        .then((res) => {
          if (res.data == true) {
            this.$message({
              message: "新增成功",
              type: "success",
            });
            this.dialogVisible = false;
          } else {
            this.$message.error("新增失败,请检查商品id是否冲突");
            this.dialogVisible = false;
          }
          console.log(res.data);
        });
    },
    handleClose(done) {
      //操作提示
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    next() {
      // 步骤条下一步的方法
      if (this.active++ > 3) this.active = 1;
    },
    // 步骤条上一步的方法
    pre() {
      if (this.active-- < 2) this.active = 1;
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    //编辑
    handleEdit(index, row) {
      console.log(index, row);
      this.edit = true;
      this.form = row;
    },
    editrd() {
      this.$axios
        .get("http://43.142.118.44/backstorg/editnotcom.php", {
          params: {
            id: this.form.id,
            name: this.form.name,
            category: this.form.category,
            de: this.form.de,
            shop: this.form.shop,
            shopid: this.form.shopid,
            address: this.form.address,
            quantity: this.form.quantity,
            comimg: this.form.comimg,
          },
        })
        .then((res) => {
          console.log(res.data);
          if (res.data == true) {
            console.log(this.form);
            this.$message.success("修改成功");
            this.selects();
            this.edit = false;
          } else {
            this.$message.error("修改失败");
          }
        });
    },
    //删除
    handleDelete(index, row) {
      console.log(index, row);
      this.$confirm("确定要删除吗", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios
            .get(`http://43.142.118.44/backstorg/delnotcom.php?id=${row.id}`)
            .then((res) => {
              if (res.data == "true") {
                this.selects();
                this.$message.success("删除成功");
              } else {
                this.$message.error("删除失败");
                this.lod = false;
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.currentPage = 1;
      this.pageSize = val;
    },
    //当前页改变时触发 跳转其他页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
    },
  },
  mounted() {
    this.selects();
  },
};
</script>
<style>
.time {
  font-size: 13px;
  color: #999;
}

.bottom {
  margin-top: 13px;
  line-height: 12px;
}

.button {
  padding: 0;
  float: right;
}

.image {
  width: 100%;
  display: block;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}
.putshelf {
  padding: 30px;
  background: #f0f2f5;
  height: 93vh;
  overflow-y: scroll;
}
.bgan {
  background: #fff;
  height: 82vh;
  margin-top: 10px;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
}
.mintitle {
  padding: 30px;
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.el-dialog {
  display: flex;
  flex-direction: column;
  margin: 0 !important;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.el-dialog .el-dialog__body {
  flex: 1;
  overflow: auto;
}

* {
  caret-color: rgba(0, 0, 0, 0);
}
</style>

